package com.sihai.ehr.repository;

import com.sihai.core.DataAccessor;
import com.sihai.core.Limit;
import com.sihai.core.Sorter;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.InviteApply;
import com.sihai.ehr.model.biz.InviteArrange;
import com.sihai.ehr.model.sys.InviteEnum;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.management.RuntimeErrorException;
import java.util.List;

/**
 * 招聘管理模块数据仓库
 * Created with IntelliJ IDEA.
 * User: henry
 * Date: 12-6-29
 * Time: 下午3:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class InviteRepository {

    @Resource
    private DataAccessor dataAccessor;

    /**
     * 根据组织关系及职务获取招聘安排数据列表
     *
     * @param organizationId
     * @param dutyId
     * @return
     */
    public List<InviteArrange> queryInviteArranges(Long organizationId, Long dutyId, InviteEnum inviteEnum, Limit limit, Sorter sorter) {

        String strSQL = "from InviteArrange i where i.cancel <> 1 ";
        if (inviteEnum != null) {
            strSQL += " and i.inviteResult='" + inviteEnum.toString() + "'";
        }
        try {
            if (organizationId < 0) {
                return dataAccessor.findByHql(InviteArrange.class,
                        strSQL, null, limit, sorter);
            } else if (dutyId < 0) {
                return dataAccessor.findByHql(InviteArrange.class,
                        strSQL + "and i.inviteApply.organization.id=?",
                        new Object[]{organizationId}, limit, sorter);
            } else {
                return dataAccessor
                        .findByHql(
                                InviteArrange.class,
                                strSQL + "and i.inviteApply.organization.id=?" + "and i.inviteApply.duty.id=?", new Object[]{
                                organizationId, dutyId}, limit, sorter);
            }
        } catch (Exception e) {
            throw new RuntimeErrorException(
                    new Error(
                            "【异常原因：根据组织关系及职务搜索招聘安排记录数据库发生异常】【发生地点：InviteRepository.queryInviteArrage;参数organizationId="
                                    + organizationId
                                    + ",dutyId="
                                    + dutyId
                                    + "】"));
        }
    }

    public void saveInviteArrange(InviteArrange inviteArrange) {
        if (inviteArrange == null) {
            throw new RuntimeException("【inviteArrange对象参数异常】");
        }
        try {
            dataAccessor.saveOrUpdate(inviteArrange);
        } catch (Exception e) {
            throw new RuntimeException("【异常原因：保存招聘安排数据库发生异常】【异常说明："
                    + e.getMessage()
                    + "】【发生地点：InviteRepository.saveInviteArrange】");
        }
    }

}
